tg-me.com/python_codes/142
Last Update:
Slugify :
Converts a string to a URL-friendly slug.
Use str.lower() and str.strip() to normalize the input string.
Use re.sub() to to replace spaces, dashes and underscores with - and remove special characters.
Code:
import re
def slugify(s):
s = s.lower().strip()
s = re.sub(r'[^\w\s-]', '', s)
s = re.sub(r'[\s_-]+', '-', s)
s = re.sub(r'^-+|-+$', '', s)
return s
EXAMPLE
slugify('Hello World!')
Output:
'hello-world'
Share and Support
@Python_Codes
BY Python Codes
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/python_codes/142